package com.cherry.web.controller.system;

import com.cherry.common.core.controller.BaseController;
import com.cherry.common.core.domain.AjaxResult;
import com.cherry.common.core.page.TableDataInfo;
import com.cherry.system.domain.SysNotice;
import com.cherry.system.service.ISysNoticeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 通知公告 控制层
 *
 * @author 孙晨阳
 */
@Controller
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController {

    private static final String PREFIX = "system/notice";

    @Resource
    private ISysNoticeService noticeService;

    /**
     * 跳转至通知公告页面
     *
     * @return 通知公告页面
     */
    @GetMapping()
    public String notice() {
        return PREFIX + "/notice";
    }

    /**
     * 查询公告列表
     *
     * @param notice 查询条件
     * @return 结果
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysNotice notice) {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 查询所有公告
     *
     * @param notice 查询条件
     * @return 结果
     */
    @PostMapping("/listAll")
    @ResponseBody
    public AjaxResult listAll(SysNotice notice) {
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return success(list);
    }

    /**
     * 新增公告
     */
    @GetMapping("/add")
    public String add() {
        return PREFIX + "/add";
    }

    /**
     * 新增保存公告
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysNotice notice) {
        notice.setCreateBy(getLoginName());
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改公告
     */
    @GetMapping("/edit/{noticeId}")
    public String edit(@PathVariable("noticeId") Long noticeId, ModelMap mmap) {
        mmap.put("notice", noticeService.selectNoticeById(noticeId));
        return PREFIX + "/edit";
    }

    /**
     * 修改保存公告
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysNotice notice) {
        notice.setUpdateBy(getLoginName());
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除公告
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(noticeService.deleteNoticeByIds(ids));
    }

    /**
     * 通知公告状态修改
     *
     * @return 结果
     */
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysNotice notice) {
        return toAjax(noticeService.changeStatus(notice));
    }
}
